<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Report_data extends MY_Controller {

	function Report_data() {
		parent::__construct();
		$this->load->model('report_model');
		$this->load->model('report_group_model');
		$this->load->model('report_data_model');
		$this->load->model('report_item_model');
	}

	public function index() {
		$result = $this->report_data_model->get_data_list();
		$datas = array();
		if(empty($result)) {
			$datas = array('report_name' => 'none data','item_name' => 'none data','data_value' => 'none data', 'data_date' => 'none data', 'update_time' => 'none data');
		} else {
			foreach($result as $row) {
				$item_id = $row['item_id'];
				$data_value = $row['data_value'];
				$data_date = $row['data_date'];
				$data_server = $row['data_server'];
				$update_time = $row['update_time'];
				$item_ids = $this->report_item_model->get_by_id($item_id);
				$item_name = $item_ids['item_name'];
				$group_id = $item_ids['group_id'];
				$group_names = $this->report_group_model->get_group_by_id($group_id);
				$group_name = $group_names['group_name'];
				$data = array('group_name' => $group_name, 'item_name' => $item_name, 'data_value' => $data_value, 'data_date' => $data_date, 'data_server' => $data_server, 'update_time' => $update_time);
				array_push($datas, $data);
			} 
		}
		if($this->session->userdata('logged_in')){
			$this->load->view('report_data', array('datas' => $datas));
		} else {
			$this->load->view('login');
		}
	}

	public function add() {
		$from_ip = $this->input->ip_address();
		switch ($from_ip)
		{
		case "120.24.166.15":
		  $data_server = "sz";
		  break;
		case "52.68.67.135":
		  $data_server = "jp";
		  break;
		case "58.96.171.124":
		  $data_server = "hk";
		  break;
		default:
		  $data_server = "none";
		}
		$salt        = "slightech";
		$item_key    = $this->input->post('key');
		$data_value  = $this->input->post('value');
		$token_fetch = $this->input->post('token');
		$data_date   = date('Y-m-d');
		$token       = md5($salt.$item_key.$data_value.$data_date);
		$row = $this->report_item_model->get_by_name($item_key,$data_server);
		$item_id = $row['item_id'];
		$res = $this->report_data_model->get_by_timekey($data_date, $item_id);
		if($token==$token_fetch){
			if (empty($res)) {
				$this->report_data_model->add($item_id, $data_value, $data_date, $data_server);
			} else {
				$this->report_data_model->update($res['data_id'], $data_value, $data_date);
			}
		}
	}

	public function delete($data_id) {
		$this->db->where('data_id', $data_id);
		$this->db->delete(self::TABLE);
		redirect("/report_data");
	}

	public function read_report_data() {
		$data_date = $this->input->get('data_date');
		$result = $this->report_data_model->get_list_by_time($data_date);
		if(empty($result)) {
			echo "none data";
		} else {
			foreach($result as $row) {
				$item_id = $row['item_id'];
				$data_value = $row['data_value'];
				$item_ids = $this->report_item_model->get_by_id($item_id);
				$item_name = $item_ids['item_name'];
				$report_id = $item_ids['report_id'];
				$report_names = $this->report_model->get_by_id($report_id);
				$report_name = $report_names['report_name'];
				$data = [
						'report_name' => $report_name,
						'item_name' => $item_name,
						'data_value' => $data_value
				];
				echo $report_name." ".$item_name." ".$data_value."<br>";
			} 
		}
		
	}

	
}